import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * The SetTime class creates the screen on which the user sets the time they want to count down from
 * @author Daniel Barg, Ivan Petrov
 *
 */

public class SetTime extends JPanel implements ActionListener{
  private MyFrame frame;
  private JLabel colon1, colon2;
  private JButton plus1, plus2, plus3, plus4, plus5, plus6;
  private JButton minus1, minus2, minus3, minus4, minus5, minus6;
  private int hoursTens, hoursOnes, minutesTens, minutesOnes, secondsTens, secondsOnes;
  private int hours, minutes, seconds;
  private JLabel tensHours, onesHours, tensMinutes, onesMinutes, tensSeconds, onesSeconds;
  private JPanel panel1, panel2, panel3, panel4, panel5, panel6;
  private JLabel info;
  private JPanel set;
  private JPanel buttons;
  private JButton left,right;

  /**
   * constructor
   * @param myFrame - frame to add the panel to
   */
  public SetTime(MyFrame myFrame) {
      frame=myFrame;
      hoursTens=0; hoursOnes=0;
      minutesTens=0; minutesOnes=0;
      secondsTens=0; secondsOnes=0;
      hours=0;minutes=0;seconds=0;
      tensHours=new JLabel(); onesHours=new JLabel(); tensMinutes=new JLabel(); onesMinutes=new JLabel(); tensSeconds=new JLabel(); onesSeconds=new JLabel();
      setJLabels();
      colon1=new JLabel(":"); colon2=new JLabel(":");
      plus1=new JButton(new ImageIcon("plus.png")); plus2=new JButton(new ImageIcon("plus.png")); plus3=new JButton(new ImageIcon("plus.png")); plus4=new JButton(new ImageIcon("plus.png")); plus5=new JButton(new ImageIcon("plus.png")); plus6=new JButton(new ImageIcon("plus.png"));
      minus1=new JButton(new ImageIcon("minus.png")); minus2=new JButton(new ImageIcon("minus.png")); minus3=new JButton(new ImageIcon("minus.png")); minus4=new JButton(new ImageIcon("minus.png")); minus5=new JButton(new ImageIcon("minus.png")); minus6=new JButton(new ImageIcon("minus.png"));
      panel1=new JPanel(new GridLayout(3,1)); panel2=new JPanel(new GridLayout(3,1)); panel3=new JPanel(new GridLayout(3,1)); panel4=new JPanel(new GridLayout(3,1)); panel5=new JPanel(new GridLayout(3,1)); panel6=new JPanel(new GridLayout(3,1));

      plus1.setAlignmentX(Component.CENTER_ALIGNMENT); plus2.setAlignmentX(Component.CENTER_ALIGNMENT); plus3.setAlignmentX(Component.CENTER_ALIGNMENT); plus4.setAlignmentX(Component.CENTER_ALIGNMENT); plus5.setAlignmentX(Component.CENTER_ALIGNMENT); plus6.setAlignmentX(Component.CENTER_ALIGNMENT);
      panel1.add(plus1); panel2.add(plus2); panel3.add(plus3); panel4.add(plus4); panel5.add(plus5); panel6.add(plus6);

      Font times=new Font("Times",1,30);
      colon1.setFont(times);colon2.setFont(times);
      tensHours.setFont(times); onesHours.setFont(times); tensMinutes.setFont(times); onesMinutes.setFont(times); tensSeconds.setFont(times); onesSeconds.setFont(times);
       panel1.add(tensHours);panel2.add(onesHours);panel3.add(tensMinutes);panel4.add(onesMinutes);panel5.add(tensSeconds);panel6.add(onesSeconds);

      panel1.add(minus1); panel2.add(minus2); panel3.add(minus3); panel4.add(minus4); panel5.add(minus5); panel6.add(minus6);

      plus1.addActionListener(this); plus2.addActionListener(this); plus3.addActionListener(this); plus4.addActionListener(this); plus5.addActionListener(this); plus6.addActionListener(this);
      plus1.setActionCommand("1"); plus2.setActionCommand("2"); plus3.setActionCommand("3"); plus4.setActionCommand("4"); plus5.setActionCommand("5"); plus6.setActionCommand("6");
      minus1.addActionListener(this); minus2.addActionListener(this); minus3.addActionListener(this); minus4.addActionListener(this); minus5.addActionListener(this); minus6.addActionListener(this);
      minus1.setActionCommand("7"); minus2.setActionCommand("8"); minus3.setActionCommand("9"); minus4.setActionCommand("10"); minus5.setActionCommand("11"); minus6.setActionCommand("12");

      this.setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
      this.add(Box.createRigidArea(new Dimension(0,25)));
      set=new JPanel();
       set.add(panel1);set.add(panel2);set.add(colon1);set.add(panel3);set.add(panel4);set.add(colon2);set.add(panel5);set.add(panel6);
      info=new JLabel(" Hours : Minutes : Seconds");
      info.setAlignmentX(Component.CENTER_ALIGNMENT);
      info.setFont(times);
      this.add(info);
      this.add(Box.createRigidArea(new Dimension(0,5)));
      this.add(set);
      this.add(Box.createRigidArea(new Dimension(0,25)));
      buttons=new JPanel();
      buttons.setLayout(new GridLayout(1,2));
      left=new JButton(new ImageIcon("left.png"));
      right=new JButton(new ImageIcon("right.png"));
      buttons.add(left);
      left.addActionListener(this);
      buttons.add(right);
      right.addActionListener(this);
      this.add(buttons);
      right.setToolTipText("timer");
      left.setToolTipText("main menu");
  }

  /**
   * actionPerformed event for buttons
   */
  public void actionPerformed(ActionEvent e) {
      if(e.getSource()==left)
          frame.mainMenu();
      else
          if (e.getSource()==right){
              if(hours>0||minutes>0||seconds>0){
                  frame.timer();
                  frame.setComplexity(hours,minutes,seconds);
              }
          }
          else{
              switch(Integer.parseInt(e.getActionCommand())){
              case 1:if(hours<51&&hoursTens<9)hoursTens++;break;
              case 2:if(hours<60&&hoursOnes<9)hoursOnes++;else{if(hoursOnes==9) {hoursOnes=0;hoursTens++;}} break;
              case 3:if(minutes<51&&minutesTens<9)minutesTens++;break;
              case 4:if(minutes<60&&minutesOnes<9)minutesOnes++;else{if(minutesOnes==9) {minutesOnes=0;minutesTens++;}} break;
              case 5:if(seconds<51&&secondsTens<9)secondsTens++;break;
              case 6:if(seconds<60&&secondsOnes<9)secondsOnes++;else{if(secondsOnes==9) {secondsOnes=0;secondsTens++;}} break;
              case 7:if(hours>9&&hoursTens>0)hoursTens--;break;
              case 8:if(hours>0&&hoursOnes>0)hoursOnes--;else{if(hoursOnes==0 && hoursTens>0) {hoursOnes=9;hoursTens--;}} break;
              case 9:if(minutes>9&&minutesTens>0)minutesTens--;break;
              case 10:if(minutes>0&&minutesOnes>0)minutesOnes--;else{if(minutesOnes==0 && minutesTens>0) {minutesOnes=9;minutesTens--;}} break;
              case 11:if(seconds>9&&secondsTens>0)secondsTens--;break;
              case 12:if(seconds>0&&secondsOnes>0)secondsOnes--;else{if(secondsOnes==0 && secondsTens>0) {secondsOnes=9;secondsTens--;}} break;
              }
              setJLabels();
          }
  }

  /**
   * sets the text of the labels on the screen
   */
  public void setJLabels(){
      tensHours.setText("  "+hoursTens+"");
      onesHours.setText("  "+hoursOnes+"");
      tensMinutes.setText("  "+minutesTens+"");
      onesMinutes.setText("  "+minutesOnes+"");
      tensSeconds.setText("  "+secondsTens+"");
      onesSeconds.setText("  "+secondsOnes+"");
      hours=hoursTens*10+hoursOnes;
      minutes=minutesTens*10+minutesOnes;
      seconds=secondsTens*10+secondsOnes;
  }

  /**
   * sets the background colors
   * @param c - color
   */
  public void setColors(Color c) {
      this.setBackground(c);
       plus1.setBackground(c);plus2.setBackground(c);plus3.setBackground(c);plus4.setBackground(c);plus5.setBackground(c);plus6.setBackground(c);
       minus1.setBackground(c);minus2.setBackground(c);minus3.setBackground(c);minus4.setBackground(c);minus5.setBackground(c);minus6.setBackground(c);
       panel1.setBackground(c);panel2.setBackground(c);panel3.setBackground(c);panel4.setBackground(c);panel5.setBackground(c);panel6.setBackground(c);
      colon1.setBackground(c); colon2.setBackground(c);
      tensHours.setBackground(c); onesHours.setBackground(c); tensMinutes.setBackground(c); onesMinutes.setBackground(c); tensSeconds.setBackground(c); onesSeconds.setBackground(c);
      info.setBackground(c);
      colon1.setForeground(frame.getOpposite(c)); colon2.setForeground(frame.getOpposite(c));
      tensHours.setForeground(frame.getOpposite(c)); onesHours.setForeground(frame.getOpposite(c)); tensMinutes.setForeground(frame.getOpposite(c)); onesMinutes.setForeground(frame.getOpposite(c)); tensSeconds.setForeground(frame.getOpposite(c)); onesSeconds.setForeground(frame.getOpposite(c));
      info.setForeground(frame.getOpposite(c));
      set.setBackground(c);
      buttons.setBackground(c);
      left.setBackground(c); right.setBackground(c);
  }

  /**
   * getter for time
   * @return -time
   */
  public int[] getTime(){
      return new int[]{hours,minutes,seconds};
  }

  /**
   * init iterates the time and resets the labels
   * @param hoursSet 
   * @param minutesSet
   * @param secondsSet
   */
  public void init(int hoursSet,int minutesSet,int secondsSet){
      hours=hoursSet;
      minutes=minutesSet;
      seconds=secondsSet;
      hoursTens=(hours-hours%10)/10;
      hoursOnes=hours%10;
      minutesTens=(minutes-minutes%10)/10;
      minutesOnes=minutes%10;
      secondsTens=(seconds-seconds%10)/10;
      secondsOnes=seconds%10;
      setJLabels();
  }
}


